﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FillCartridgeBBB.Models;

namespace FillCartridgeBBB.Controllers
{
    public class CartridgeController : Controller
    {
        private DataManager _dataManager;
        
        public CartridgeController (DataManager dataManager)
        {
            _dataManager = dataManager;
        }

        public ActionResult List()
        {
            ViewData["Cartridges"] = _dataManager.Cartridges.SelectAll();
            return View();
        }

        //
        // GET: /Cartridge/Create

        public ActionResult Create()
        {
            ViewData["States"] = new SelectList(_dataManager.States.SelectAll(), "StateId", "StateName");
            ViewData["Printers"] = new SelectList(_dataManager.Printers.SelectAll(), "PrinterId", "PrinterName"); 
            return View();
        } 

        //
        // POST: /Cartridge/Create

        [HttpPost]
        public ActionResult Create(Cartridge cartridge, int PrinterId, int StateId)
        {
            cartridge.Printer = _dataManager.Printers.SelectById(PrinterId);
            cartridge.State = _dataManager.States.SelectById(StateId);
                if (cartridge.SerialNumber == null)
                {
                    ModelState.AddModelError("SerialNumber", "Введіть серійний номер");
                }
                if (ModelState.IsValid)
                {
                    _dataManager.Cartridges.Insert(cartridge);
                    return RedirectToAction("List");
                }

                    ViewData["States"] = new SelectList(_dataManager.States.SelectAll(), "StateId", "StateName");
                    ViewData["Printers"] = new SelectList(_dataManager.Printers.SelectAll(), "PrinterId", "PrinterName");
                    return View();
            
        }

        //
        // GET: /Cartridge/Delete/5
 
        public ActionResult Delete(int id)
        {
            _dataManager.Cartridges.Delete(id);
            return RedirectToAction("List");
        }

        //
        // GET: /Cartridge/Edit/5
 
        public ActionResult Edit(int id)
        {
            Cartridge cartridge = _dataManager.Cartridges.SelectById(id);
            ViewData.Model = cartridge;
            ViewData["States"] = new SelectList(_dataManager.States.SelectAll(), "StateId", "StateName", cartridge.State.StateId);
            ViewData["Printers"] = new SelectList(_dataManager.Printers.SelectAll(), "PrinterId", "PrinterName", cartridge.Printer.PrinterId); 
            return View();
        }

        //
        // POST: /Cartridge/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, Cartridge cartridge, int PrinterId, int StateId)
        {
            if (cartridge.SerialNumber == null)
            {
                ModelState.AddModelError("SerialNumber", "Введіть серійний номер");
            }
            if (ModelState.IsValid)
            {
                cartridge.CartridgeId = id;
                _dataManager.Cartridges.Update(cartridge, PrinterId, StateId);
                return RedirectToAction("List");
            }

            ViewData["States"] = new SelectList(_dataManager.States.SelectAll(), "StateId", "StateName", cartridge.State.StateId);
            ViewData["Printers"] = new SelectList(_dataManager.Printers.SelectAll(), "PrinterId", "PrinterName", cartridge.Printer.PrinterId); 
            return View();
        }
    }
}
